﻿/****************************** Module Header ******************************\
Module Name:    ErrorLogEvent.cs
Project:        SQL Server Error Log Parsing module

Instances of this class are written to the output pipeline by the 
Get-ErrorLogEvent Cmdlet.

This source is subject to the Microsoft Public License.
See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
All other rights reserved.

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace SQLServerSleuth.PowerShell.Commands.ParseERRORLOG
{
    /// <summary>
    /// ErrorLogEvent objects, which represent the components of an entry in 
    /// the SQL Server error log, are emitted by the Get-ErrorLogEvent 
    /// Cmdlet.
    /// </summary>
    public class ErrorLogEvent
    {
        /// <summary>
        /// The solitary constructor for the class assigns the given values 
        /// to the private fields.
        /// </summary>
        /// <param name="EventTime">The time at which the error log event 
        /// occurred.</param>
        /// <param name="SessionID">The ID (SPID) of the SQL Server session 
        /// that registered the event, if available.</param>
        /// <param name="SessionType">An enumeration that indicates that the 
        /// SQL Server session was from a specific subsystem.</param>
        /// <param name="MessageID">The ID of the parsed SQL Server system 
        /// message, if the system message was successfully parsed.</param>
        /// <param name="MessageSeverity">The severity of the parsed 
        /// SQL Server system message, if the system message was 
        /// successfully parsed.</param>
        /// <param name="MessageParameters">A collection of parameter values 
        /// that replaced tokens in the corresponding message template.</param>
        /// <param name="SourceLineNumbers">A collection of the line numbers 
        /// on which the entry resides in the error log file.</param>
        /// <param name="MessageText">The unparsed text of the error log 
        /// entry.</param>
        /// <param name="ErrorLogPath">The path to SQL Server error log file 
        /// from which the entry came.</param>
        public ErrorLogEvent
        (
            DateTime EventTime,
            int SessionID,
            SQLServerSessionTypes SessionType,
            int MessageID,
            byte MessageSeverity,
            string[] MessageParameters,
            int[] SourceLineNumbers,
            string MessageText,
            string ErrorLogPath
        )
        {
            this._eventTime = EventTime;
            this._sessionID = SessionID;
            this._sessionType = SessionType;
            this._messageID = MessageID;
            this._messageSeverity = MessageSeverity;
            this._messageParameters = MessageParameters;
            this._sourceLineNumbers = SourceLineNumbers;
            this._messageText = MessageText;
            this._errorLogPath = ErrorLogPath;

            return;
        }

        /// <summary>
        /// See the corresponding parameter of the <see cref="ErrorLogEvent"/> constructor.
        /// </summary>
        public string ErrorLogPath
        {
            get
            {
                return this._errorLogPath;
            }
        }
        private string _errorLogPath;

        /// <summary>
        /// See the corresponding parameter of the <see cref="ErrorLogEvent"/> constructor.
        /// </summary>
        public DateTime EventTime
        {
            get
            {
                return this._eventTime;
            }
        }
        private DateTime _eventTime;

        /// <summary>
        /// See the corresponding parameter of the <see cref="ErrorLogEvent"/> constructor.
        /// </summary>
        public Nullable<int> MessageID
        {
            get
            {
                if (0 != this._messageID)
                {
                    return this._messageID;
                }
                else
                {
                    return null;
                }
            }
        }
        private int _messageID;

        /// <summary>
        /// See the corresponding parameter of the <see cref="ErrorLogEvent"/> constructor.
        /// </summary>
        public Nullable<byte> MessageSeverity
        {
            get
            {
                //  The check of the _messageID field is intentional... a 
                //  severity value of zero is valid, so that cannot be used 
                //  to infer whether null should be returned.  On the other 
                //  hand, a _messageID value of zero is invalid and clearly 
                //  indicates that the error log entry could not be 
                //  successfully parsed.
                if (0 != this._messageID)
                {
                    return this._messageSeverity;
                }
                else
                {
                    return null;
                }
            }
        }
        private byte _messageSeverity;

        /// <summary>
        /// See the corresponding parameter of the <see cref="ErrorLogEvent"/> constructor.
        /// </summary>
        public string MessageText
        {
            get
            {
                return this._messageText;
            }
        }
        private string _messageText;

        /// <summary>
        /// See the corresponding parameter of the <see cref="ErrorLogEvent"/> constructor.
        /// </summary>
        public string[] MessageParameters
        {
            get
            {
                return this._messageParameters;
            }
        }
        private string[] _messageParameters;

        /// <summary>
        /// See the corresponding parameter of the <see cref="ErrorLogEvent"/> constructor.
        /// </summary>
        public Nullable<int> SessionID
        {
            get
            {
                if (0 != this._sessionID)
                {
                    return this._sessionID;
                }
                else
                {
                    return null;
                }
            }
        }
        private int _sessionID;

        /// <summary>
        /// See the corresponding parameter of the <see cref="ErrorLogEvent"/> constructor.
        /// </summary>
        public SQLServerSessionTypes SessionType
        {
            get
            {
                return this._sessionType;
            }
        }
        private SQLServerSessionTypes _sessionType;

        /// <summary>
        /// See the corresponding parameter of the <see cref="ErrorLogEvent"/> constructor.
        /// </summary>
        public int[] SourceLineNumbers
        {
            get
            {
                return this._sourceLineNumbers;
            }
        }
        private int[] _sourceLineNumbers;
    }
}
